﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.Models;

public partial class FileManager_FileAttribute : System.Web.UI.Page
{
    List<AccFile> list;
    FileInfo file1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            if (id != null)
            {
                list = new AccFileManager().GetAllAccFileByFileId(Convert.ToInt32(id));
                this.GridView1.DataSource = list;
                this.GridView1.DataBind();
                file1 = new FileInfoManager().GetAllFileInfoByFileId(Convert.ToInt32(id));
                this.txtFileName.Text = file1.FileName;
                this.lbPosition.Text = file1.FilePath;
                this.txtRemark.Text = file1.Remark;
                this.lbCreateDate.Text = file1.CreateDate.ToString();
                this.lbOwner.Text = file1.FileOwner;
                CheckType(file1);
            }
            else
            {
                string address = Request.QueryString["pos"];
                this.lbPosition.Text = address;
               // this.lbOwner.Text = Session["username"].ToString();
            }





        }
    }

    private void CheckType(FileInfo file)
    {
        switch (file.FileType.FileTypeId)
        {
            case 2: this.rbExcel.Checked = true;
                break;
            case 3: this.rbWord.Checked = true;
                break;
            case 4: this.rbPPT.Checked = true;
                break;
            case 5: this.rbAcc.Checked = true;
                break;
            case 6: this.rbIE.Checked = true;
                break;
            case 8: this.rbPdf.Checked = true;
                break;
        }
    }

    //protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    GridViewRow dv = (GridViewRow)(((ImageButton)(e.CommandSource)).Parent.Parent);
    //    int id =Convert.ToInt32(GridView1.DataKeys[dv.RowIndex].Value);
    //}
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        int fileId = Convert.ToInt32(Request.QueryString["id"]);
        new AccFileManager().DeleteAccFile(id, fileId);
        list = new AccFileManager().GetAllAccFileByFileId(Convert.ToInt32(Request.QueryString["id"]));
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }

  
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            FileInfo file = new FileInfo();
            file.CreateDate = Convert.ToDateTime(this.lbCreateDate.Text);
            file.FileId = Convert.ToInt32(Request.QueryString["id"]);
            file.FileName = this.txtFileName.Text;
            file.FileOwner = this.lbOwner.Text;
            file.FilePath = this.lbPosition.Text;
            FileTypeInfo type = new FileTypeInfo();
            type.FileTypeId = rbChecked();
            file.FileType = type;
            file.IfDelete = file1.IfDelete;
            file.ParentId = file1.ParentId;
            file.Remark = this.txtRemark.Text;
            new FileInfoManager().UpdateFileInfo(file);
            Response.Redirect("FileManager.aspx");
        }
        else
        {

            Response.Redirect("FileManager.aspx");
        }
       
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            Response.Redirect("FileManager.aspx");
        }
        else
        {
            Response.Redirect("FileManager.aspx");
        }
       
    }
    protected void btnUp_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            AccFile acc = new AccFile();
            int code = FileUpload1.FileName.IndexOf(".");
            int length = FileUpload1.FileName.Length;
            string fileName = FileUpload1.FileName.Substring(0, code);
            string exp = FileUpload1.FileName.Substring(code + 1, 3);
            string filePath = Server.MapPath("../File");
            acc.AccName = fileName;
            acc.AccPath = filePath + "\\" + FileUpload1.FileName;
            acc.AccSize = FileUpload1.PostedFile.ContentLength;
            acc.CreateDate = DateTime.Now;
            acc.FileId = Convert.ToInt32(Request.QueryString["id"]);
            FileTypeInfo type = new FileTypeInfo();
            type.FileTypeId = GetTypeId(exp);
            acc.AccType = type;
            new AccFileManager().AddAccFile(acc);
            list = new AccFileManager().GetAllAccFileByFileId(Convert.ToInt32(Request.QueryString["id"]));
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
            FileUpload1.SaveAs(filePath + "/" + FileUpload1.FileName);
        }
        else
        {
            //先添加文件
            FileInfo file = new FileInfo();
            file.CreateDate = DateTime.Now;
           file.FileName = this.txtFileName.Text;
            file.FileOwner ="admin";
            file.FilePath = this.lbPosition.Text;
            FileTypeInfo type = new FileTypeInfo();
            type.FileTypeId = rbChecked();
            file.FileType = type;
            file.IfDelete =0;
            file.ParentId = Convert.ToInt32(Request.QueryString["nodeid"]);
            file.Remark = this.txtRemark.Text;
            new FileInfoManager().AddFileInfo(file);
             int maxfileId= GetMaxId();//取出刚添加的文件ID

            //再添加附件
            AccFile acc = new AccFile();
            int code = FileUpload1.FileName.IndexOf(".");
            int length = FileUpload1.FileName.Length;
            string fileName = FileUpload1.FileName.Substring(0, code);
            string exp = FileUpload1.FileName.Substring(code + 1, 3);
            string filePath = Server.MapPath("../File");
            acc.AccName = fileName;
            acc.AccPath = filePath + "\\" + FileUpload1.FileName;
            acc.AccSize = FileUpload1.PostedFile.ContentLength;
            acc.CreateDate = DateTime.Now;
            acc.FileId = maxfileId;
            FileTypeInfo type1 = new FileTypeInfo();
            type1.FileTypeId = GetTypeId(exp);
            acc.AccType = type1;
            new AccFileManager().AddAccFile(acc);
            list = new AccFileManager().GetAllAccFileByFileId(maxfileId);
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
            FileUpload1.SaveAs(filePath + "/" + FileUpload1.FileName);
        }
       
    }

    private int GetMaxId()
    {
        int fileId = 0;
        foreach (FileInfo item in new FileInfoManager().GetAllFileInfo())
        {
            if (fileId == 0)
            {
                fileId = item.FileId;
            }
            else
            {
                if (fileId < item.FileId)
                {
                    fileId = item.FileId;
                }
            }
        }
        return fileId;
    }
    private int GetTypeId(string exp)
    {
        int result = 0;
        switch (exp)
        {
            case "xls": result = 2;
                break;
            case "doc": result = 3;
                break;
            case "ppt": result = 4;
                break;
            case "mdb": result = 5;
                break;
            case "html": result = 6;
                break;
            case "pdf": result = 8;
                break;
        }
        return result;
    }
    private int rbChecked()
    {
        if (this.rbAcc.Checked == true)
        {
            return 5;
        }
        if (this.rbExcel.Checked == true)
        {
            return 2;
        }
        if (this.rbIE.Checked == true)
        {
            return 6;
        }
        if (this.rbPdf.Checked == true)
        {
            return 8;
        }
        if (this.rbWord.Checked == true)
        {
            return 3;
        }
        if (this.rbPPT.Checked == true)
        {
            return 4;
        }
        return -1;
    }
}